package com.mobi.Survey.lib.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class LoadSumData {
	int returnCode = 0;
	String surveyKey ="";
		List<AggregateResultPojo> sumDataSet = new ArrayList<AggregateResultPojo>();

		//List<AggregateResultPojo> sumDataSet = null;
		AggregateResultPojo survey = null;

 
  
	
public List<AggregateResultPojo> getSurveyGraphData(String urlPath) {
	returnCode = -1;
 
	HttpParams myParms = new BasicHttpParams();
	HttpConnectionParams.setConnectionTimeout(myParms, 100000);
	HttpConnectionParams.setSoTimeout(myParms, 100000);
	DefaultHttpClient defaultHttpClient = new DefaultHttpClient(myParms);

	String results;
	try {
		HttpGet httpGet = new HttpGet(urlPath);
		HttpResponse httpResponse = defaultHttpClient.execute(httpGet); 

		results = EntityUtils.toString(httpResponse.getEntity());
		//String rowResult = JSONParseString(results);
		sumDataSet = JSONParseRecordSet(results);

	 } catch (Exception je)	 {
		 //welcomeMessage.setText("Error: Web Service not working: "+je.getMessage());
     	returnCode = -1;
	 }	
	return sumDataSet;
}	


	
	private List<AggregateResultPojo> JSONParseRecordSet(String jsontext) {
		int row = 0;
	       try {
	            //SharedPreferences surveySettings = getSharedPreferences(PREFERENCE_FILE, MODE_PRIVATE);  
	       	    //SharedPreferences.Editor prefEditor = surveySettings.edit();  
	       	    
				sumDataSet = new ArrayList<AggregateResultPojo>();
	       	    JSONObject myjson = new JSONObject(jsontext);
	            JSONArray nameArrayx = myjson.names();
	            String recordSetName = (String) nameArrayx.get(0);
	            JSONArray recordArray = myjson.getJSONArray(recordSetName);
	
				JSONObject obj1= (JSONObject) recordArray.get(0);
	            JSONArray colNames = obj1.names();
	            String[] colName = new String [colNames.length()];
	            for(int i=0;i<colNames.length();i++)
	            {
	            	colName[i] = colNames.getString(i);
	            }
	            
	            JSONObject rowObject;
	            JSONArray valArrayx;
	            row =recordArray.length(); 

	            for(int i=0;i<=recordArray.length()-1;i++)
	            {
					rowObject= (JSONObject) recordArray.get(i);
					valArrayx = rowObject.toJSONArray(colNames);
					AggregateResultPojo sumData = new AggregateResultPojo();
		            for(int k=0;k<valArrayx.length();k++)
		            {
			            // Save record set values in a file on Android
		            	//prefEditor.putString(colName[k]+"-"+i,  valArrayx.getString(k)); 
		            	if (colName[k] != null && colName[k].equals("FormId")) {
		            		sumData.setSurveyId(Integer.parseInt(valArrayx.getString(k)));
		            	} else 	if (colName[k] != null && colName[k].equals("ControlType")) {
		            		sumData.setCtrlType(Byte.parseByte(valArrayx.getString(k)));
		            	} else 	if (colName[k] != null && colName[k].equals("ControlText")) {
		            		sumData.setTitle(valArrayx.getString(k));
		            	} else 	if (colName[k] != null && colName[k].equals("ControlValue")) {
		            		sumData.setRating(valArrayx.getString(k));
		            	} else 	if (colName[k] != null && colName[k].equals("FormDetailsPK")) {
		            		sumData.setSequanceNo(Integer.parseInt(valArrayx.getString(k)));
		            	} else 	if (colName[k] != null && colName[k].equals("rowCount")) {
			            		sumData.setCount(Double.parseDouble(valArrayx.getString(k)));
		            	}
		            }
		            sumData.setSurveyKey(surveyKey);
					sumDataSet.add(sumData);
	            }
	        	//prefEditor.putInt("FormRows", row); 
	        	//prefEditor.commit();        	     	    
	     	    
	        } catch (JSONException e) {
	                e.printStackTrace();
	        }

	return sumDataSet ;
	}	
	
}
